package com.aptoto.thrift.alone;

import java.io.Serializable;
import java.util.Collections;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.Map;

import org.apache.thrift.TBase;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.scheme.IScheme;
import org.apache.thrift.scheme.SchemeFactory;
import org.apache.thrift.scheme.StandardScheme;

import org.apache.thrift.meta_data.FieldMetaData;

public class ThriftDynamicStruct
		implements TBase<ThriftDynamicStruct, ThriftDynamicFieldIdEnum>, Serializable, Cloneable, Comparable<ThriftDynamicStruct> {
	private static final long serialVersionUID = 7251832441350823910L;
	private final String structIdentifier;

	public ThriftDynamicStruct(String structIdentifier) {
		super();
		this.structIdentifier = structIdentifier;
		
		Map<ThriftDynamicFieldIdEnum, FieldMetaData> metaDataMap = new HashMap<ThriftDynamicFieldIdEnum, FieldMetaData>();
		metaDataMap = Collections.unmodifiableMap(metaDataMap);
		FieldMetaData.addStructMetaDataMap(ThriftDynamicStruct.class, metaDataMap);
	}

	@Override
	public void read(TProtocol iprot) throws TException {
		scheme(structIdentifier, iprot).read(iprot, this);
	}

	@Override
	public void write(TProtocol oprot) throws TException {
		// TODO Auto-generated method stub

	}

	@Override
	public int compareTo(ThriftDynamicStruct o) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public ThriftDynamicFieldIdEnum fieldForId(int fieldId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean isSet(ThriftDynamicFieldIdEnum field) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public Object getFieldValue(ThriftDynamicFieldIdEnum field) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void setFieldValue(ThriftDynamicFieldIdEnum field, Object value) {
		// TODO Auto-generated method stub

	}

	@Override
	public ThriftDynamicStruct deepCopy() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void clear() {
		// TODO Auto-generated method stub

	}

	/**
	 * **************************************************************
	 */

	private static <S extends IScheme> S scheme(String structIdentifier, TProtocol proto) {
		SchemeFactory chemeFactory = null;
		if (StandardScheme.class.equals(proto.getScheme())) {
			chemeFactory = new SchemeFactory() {
				@Override
				public ThriftDynamicStandardScheme getScheme() {
					return new ThriftDynamicStandardScheme(structIdentifier);
				}
			};
		} else {
			chemeFactory = new SchemeFactory() {
				@Override
				public ThriftDynamicTupleScheme getScheme() {
					return new ThriftDynamicTupleScheme();
				}
			};
		}
		
		return chemeFactory.getScheme();
	}
}
